package app.kotlin_1.utils

import android.animation.Animator
import android.animation.TypeEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.PointF
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout

/**
 * Created by 刘智强 on 2017/9/29.
 */

object DownloadAnimation {
    private var downloadAnimationListener: DownloadAnimationListener? = null
    fun setDownloadAnimationListener(downloadAnimationListener: DownloadAnimationListener) {
        DownloadAnimation.downloadAnimationListener = downloadAnimationListener
    }

    /**
     * 仿作美团外卖的购物车动画
     *
     * @param startView 动画的起点位置
     * @param endView   动画的终点位置
     * @param mContext
     * @param rl        父窗体 用于添加的动画的View
     * @param time      动画持续时间单位s
     */
    fun AddToCart(startView: ImageView, endView: View, mContext: Context, rl: RelativeLayout, time: Int) {
        //新建一个ImageView 用于动画显示
        val view = ImageView(mContext)
        //确定ImageView大小与传进来的ImageView相同
        val params = RelativeLayout.LayoutParams(startView.width, startView.height)
        //获取ImageView的图片 并设置在新的ImageView上
        view.setImageDrawable(startView.drawable)

        //计算父控件的位置
        val parent = IntArray(2)
        rl.getLocationInWindow(parent)

        //计算起点控件位置
        val startLocation = IntArray(2)
        startView.getLocationInWindow(startLocation)

        //计算终点控件位置
        val endLocation = IntArray(2)
        endView.getLocationInWindow(endLocation)

        //确定ImageView的位置与startView相同
        params.leftMargin = startLocation[0] - parent[0] - rl.paddingLeft
        params.topMargin = startLocation[1] - parent[1] - rl.paddingTop
        rl.addView(view, params)

        //计算两者的横向X轴的距离差
        val XtoX = endLocation[0] - startLocation[0] + endView.width / 2 - startView.width / 2
        //根据距离 时间 获取到对应的X轴的初速度
        val xv = (XtoX / time).toFloat()

        //计算两者的横向X轴的距离差
        val YtoY = endLocation[1] - startLocation[1]
        //根据距离 时间 初始设置的Y轴初速度与X轴初速度相同 获取到竖直方向上的加速度
        val g: Float
        if (xv > 0) {
            g = (YtoY + xv * time) / time.toFloat() / time.toFloat() * 2
        } else {
            g = (YtoY - xv * time) / time.toFloat() / time.toFloat() * 2
        }
        val va = ValueAnimator()
        va.duration = (time * 1000).toLong()
        va.setObjectValues(PointF(0f, 0f))
        //计算位置
        va.setEvaluator { v, pointF, t1 ->
            val point = PointF()
            point.x = v * xv * time.toFloat()
            if (xv > 0) {
                point.y = g * (v * time) * (v * time) / 2 - xv * v * time.toFloat()
            } else {
                point.y = g * (v * time) * (v * time) / 2 + xv * v * time.toFloat()
            }
            point
        }
        va.start()
        //设置动画
        va.addUpdateListener { valueAnimator ->
            val point = valueAnimator.animatedValue as PointF
            view.translationX = point.x
            view.translationY = point.y
        }

        //在动画结束时去掉动画添加的ImageView
        va.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animator: Animator) {

            }

            override fun onAnimationEnd(animator: Animator) {
                rl.removeView(view)
                if (downloadAnimationListener != null) {
                    downloadAnimationListener!!.onAnimationEnd()
                }

            }

            override fun onAnimationCancel(animator: Animator) {

            }

            override fun onAnimationRepeat(animator: Animator) {

            }
        })
    }

    interface DownloadAnimationListener {
         fun onAnimationEnd()
     }



}
